package com.lfg.lintcode;


import java.util.Arrays;


public class No267MinimumRestDays {
    public static void main(String[] args) {
        int ret = minimumRestDays(new int[]{1, 1, 0, 0}, new int[]{0, 1, 1, 0});
        System.out.println("[" + ret + "]");
    }

    public static int minimumRestDays(int[] company, int[] gym) {
        int length = company.length;
        int[][] dp = new int[3][length];

        Arrays.fill(dp[0], Integer.MAX_VALUE);
        Arrays.fill(dp[1], Integer.MAX_VALUE);
        Arrays.fill(dp[2], Integer.MAX_VALUE);


        dp[0][0] = 1;
        if (company[0] == 1) {
            dp[1][0] = 0;
        } else {
            dp[1][0] = 1;
        }


        if (gym[0] == 1) {
            dp[2][0] = 0;
        } else {
            dp[2][0] = 1;
        }

        for (int i = 1; i < length; i++) {
            dp[0][i] = Math.min(Math.min(dp[0][i - 1], dp[1][i - 1]), dp[2][i - 1]) + 1;
            if (company[i] == 1) {
                dp[1][i] = Math.min(dp[0][i - 1], dp[2][i - 1]);
            }
            if (gym[i] == 1) {
                dp[2][i] = Math.min(dp[0][i - 1], dp[1][i - 1]);
            }

        }
        int min = Math.min(dp[0][dp.length], dp[1][dp.length]);
        return Math.min(dp[2][dp.length], min);
    }

}
